//
//  ViewController.swift
//  SwiftMusicPlayer
//
//  Created by 顾佳槟 on 2018/3/7.
//  Copyright © 2018年 lancoo. All rights reserved.
//

import UIKit

class ViewController: BaseViewController {
    
    //懒加载主界面
    fileprivate lazy var mainView:MainView = {
        let _mainView = MainView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - kVideoPlayerHeight))
        
        //滚动结束Block
        _mainView.mainScrollView?.scrollViewEndScrollBlock = {[weak self](index:Int)->Void in
            self?.mainTitleToolView.switchToButton(index: index + 1)
        }
        
        //滚动进行中Block
        _mainView.mainScrollView?.scrollViewDidScrollBlock = {[weak self](XOffset:CGFloat)->Void in
            let ratio = Float((self?.mainTitleToolView.recommendButton?.center.x)! - (self?.mainTitleToolView.owerBUtton?.center.x)!)/Float((self?.mainView.frame.width)!)
            self?.mainTitleToolView.moveLineLabel(centerXOffset: XOffset * CGFloat(ratio),animated: false)
        }
        
        return _mainView
    }()
    
    //懒加载播放界面
    fileprivate lazy var playerView:VideoPlayerView = {
        let _playerView = VideoPlayerView.init(frame:CGRect.zero)
        
        return _playerView
    }()
    
    //懒加载标题工具界面
    fileprivate lazy var mainTitleToolView:MainTitleToolView = {
        let _mainTitleToolView = MainTitleToolView.init(frame:CGRect.init(x: 0, y: 0, width: 180, height: 44))
        
        //工具按钮点击Block
        _mainTitleToolView.buttonDidClickBlock = {[weak self](tag:Int)->Void in
            self?.mainView.mainScrollView?.scrollToView(index: tag)
        }
        
        return _mainTitleToolView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupSubViews()
        config()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension ViewController{
    
    //加载子控件
    func setupSubViews() {
        self.view.addSubview(self.mainView)
        
        self.navigationController?.view.addSubview(self.playerView)
        self.playerView.mas_makeConstraints { [weak self](make) in
            make?.bottom.equalTo()(self?.navigationController!.view)
            make?.left.equalTo()(self?.navigationController!.view)
            make?.right.equalTo()(self?.navigationController!.view)
            make?.height.setOffset(kVideoPlayerHeight)
        }
        
        self.navigationItem.titleView = self.mainTitleToolView
    }
    
    //数据配置
    func config() {
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "SearchIcon"), style: .done, target: self, action: #selector(search))
        self.navigationItem.rightBarButtonItem?.tintColor = UIColor.init(hexString: "4E4E4E")
    }
    
    @objc func search() {
        
    }
}

